package com.jeeplus.modules.sys.SSO.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.modules.sys.SSO.utiks.OkHttpUtil;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.UserService;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
//@Api(tags = "单点登录_用户")
@RequiredArgsConstructor
@RequestMapping(value = "/SSOUser")
@EnableScheduling
public class SSOUserController{
    @Value("${LoginID}")
    private String LoginID;
    @Value("${LoginPWD}")
    private String LoginPWD;
    @Value("${LoginUrl}")
    private String LoginUrl;
    @Value("${UserUrl}")
    private String UserUrl;
    @Value("${Cid}")
    private String Cid;
    @Value("${Oid}")
    private String Oid;
    @Value("${Rid}")
    private String Rid;



    @Autowired
    private UserService userService;


    @GetMapping("getAndSaveSSoUser")
    //@ApiOperation(value = "获取用户并添加")
    public AjaxJson getAndSaveSSoUser(){
        //调用登录接口获取token
        String s = OkHttpUtil.getToken(LoginID, LoginPWD, LoginUrl);
        JSONObject jsonObject = JSONObject.fromObject(s);
        if (jsonObject.has("Data")) {
            //获取token
           String token=jsonObject.getString("Data");
           //组装请求条件
            JSONObject condition = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            condition.put("where",jsonArray);
            condition.put("OrderType","true");
            condition.put("OrderField","EmployeeID");
            condition.put("PageSize",30);
            condition.put("PageCurrent",1);
            //抓取第一遍获取总页数
            String ssoUser = OkHttpUtil.getSSOUser(token, condition.toString(), UserUrl);
            JSONObject fromObject = JSONObject.fromObject(ssoUser);
            Object PageCount = JSONObject.fromObject(fromObject.get("Data")).get("PageCount");
            if (null !=PageCount && !"".equals(PageCount)){
                List<String> all = userService.findAll();
                int i = (int) PageCount;
                for (int j = 1; j <= i; j++) {
                    condition.put("PageCurrent",j);
                     ssoUser = OkHttpUtil.getSSOUser(token, condition.toString(), UserUrl);
                    JSONArray pageList = JSONArray.fromObject(JSONObject.fromObject(JSONObject.fromObject(ssoUser).get("Data")).get("PageList"));

                    for (Object o : pageList) {
                        User postting = postting(o);
                       boolean isTrue=true;
                        for (String id : all) {
                            if( postting.getId().equals(id)){
                                isTrue=false;
                            }

                        }
                        if(isTrue){
                            userService.addUser(postting);
                        }else{
                            userService.updateUserInfo(postting);
                        }
                    }

                }
            }


        }

        return AjaxJson.success("用户添加成功");
    }
    public User postting(Object o){
        User user=new User();
        //把获取到的数据放入对象
        JSONObject object = JSONObject.fromObject(o);
        if(object.has("EmployeeID")){
            //唯一标识
            user.setId(object.getString("EmployeeID"));
        }
        if(object.has("EmployeeName")){
            //名称
            user.setName(object.getString("EmployeeName"));

        }
        if(object.has("EmployeePhoto")){
            //照片
            user.setPhoto(object.getString("EmployeePhoto"));
        }
        if(object.has("ContactNumber")){
            //联系电话
            user.setPhone(object.getString("ContactNumber"));
        }
        if(object.has("ContactMail")){
            //联系邮箱
            user.setEmail(object.getString("ContactMail"));
        }
        if(object.has("LoginID")){
            //登录账号
            user.setLoginName(object.getString("LoginID"));
        }
            //登录密码
            user.setPassword(userService.entryptPassword("123456"));
        if(object.has("IsReadOnly")){
            //禁用 Bool
            if(object.getString("IsReadOnly").equals("true")){
                user.setLoginFlag("0");
            }else{
                user.setLoginFlag("1");
            }

        }
      user.setRemarks("对接单点用户");
        Office office=new Office();
        office.setId(Cid);
        user.setCompany(office);
        office.setId(Oid);
        user.setOffice(office);
        List<Role> list=new ArrayList<>();
        Role role=new Role();
        role.setId(Rid);
        list.add(role);
        user.setRoleList(list);


        return user;
    }

}
